﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;
using Imaginecup.Recycling.GameMan;
using Imaginecup.Recycling.Object;
using Imaginecup.recycling.Object.Item;
using Imaginecup.recycling.Object;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.recycling.Factory;
using Imaginecup.util.input;
using Imaginecup.recycling.GameMan;
using Microsoft.Xna.Framework.Input;
using Imaginecup.util;
using Imaginecup.Engine;
using Microsoft.Xna.Framework.Input.Touch;
using Imaginecup.gameState.recycling.GameMan;
using Imaginecup.gameState.recycling.Entity;
using Imaginecup.util.StaticValue;
using Imaginecup.recycling.Object.Menu;

namespace Imaginecup.recycling.Stage
{
    // 각 스테이지별로 구성되어 있지만 여기서 한번더 정보클래스를 체인지 할 수 있음
    public class Stage1 : BaseStage, IStage, IStageBaseBehavior
    {
        //private TouchTrailLayer m_TrailLayer;
        //private ItemManager m_InventoryManager;
        private MenuManager m_MenuManager;

        public Stage1()
        {
            //사용할 리소스 초기화
            this.Initialize();
            //맵 초기화
            TileMap.ChangeStage_Init(new StageInfo3());
        }
        public Stage1(StageID stageID)
        {
            //사용할 리소스 초기화
            this.Initialize();
            //맵 초기화
            //TileMap.ChangeStage_Init();
        }

        protected override void Initialize()
        {
            m_GameManager = new GameManager();
            //m_Manager.Initialize();

//            m_TrailLayer = TouchTrailLayer.GetTouchTrailLayer();
//            m_InventoryManager = new ItemManager();

            IObjectFactory factory = ObjectFactory.GetInstance();

            m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND_BACK));
            m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND));
            //m_GameManager.AddObject(factory.GetObject(ObjectID.BACKGROUND_FRONT));

            IGameObject obj = factory.GetObject(ObjectID.PLAYER);
            m_GameManager.AddObject(obj);

            m_MenuManager = new MenuManager((IPlayer)obj);            
            TileMap.SetItemManager(m_MenuManager);
            

#if WINDOWS_PHONE
            m_GameManager.AddObject(factory.GetObject(ObjectID.JUMPBTN));
            m_GameManager.AddObject(factory.GetObject(ObjectID.SLIDINGBTN));
#endif
        }

        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            base.Draw(spriteBatch, gameTime);
            //타일맵 그리기
            TileMap.Draw(spriteBatch);
            //m_InventoryManager.Draw(spriteBatch, gameTime);
            //m_MenuManager.Draw(spriteBatch, gameTime);
            RenderDrawer.GetInstance().Drawer(spriteBatch, gameTime);
//            m_TrailLayer.Draw();
        }

#if XBOX
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            m_InventoryManager.Update(gameTime);
        }
#endif
#if WINDOWS
        private bool m_bMousePress = false;
#endif
        public override void Update(GameTime gameTime)
        {
#if WINDOWS
#if FALSE
            MouseState currentMouseState = MouseInputHandler.GetInputHandler().MouseState;
            MouseState prevMouseState = MouseInputHandler.GetInputHandler().PreviousMouseState;

            if (prevMouseState.LeftButton == ButtonState.Released && currentMouseState.LeftButton == ButtonState.Pressed)
            {
                m_bMousePress = true;
                m_TrailLayer.TouchBegan(currentMouseState.X, currentMouseState.Y, ResourceManager.GetInstance().GetContent("streak" + RandomUtil.rand.Next(1, 5)), 10);
            }
            else if (prevMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released)
            {
                m_bMousePress = false;
                m_TrailLayer.TouchEnd();
            }
            else if (m_bMousePress)
            {
                m_TrailLayer.TouchMove(currentMouseState.X, currentMouseState.Y);
            }
#endif
#endif

            base.Update(gameTime);
//            m_InventoryManager.Update(gameTime);
//            m_MenuManager.Update(gameTime);
            Updater.GetInstance().Update(gameTime);
//            m_TrailLayer.Update();

            if (m_StageManager.IsGameOver())
            {
                m_StageManager.ChangeStage(StageID.SELECT_STAGE);
                if (SelectStageItemInfo.OpenStageCount < StaticValues.MAX_STAGE)
                    SelectStageItemInfo.OpenStageCount++;
            }

            if (m_StageManager.IsGameClear())
            {
                m_StageManager.ChangeStage(StageID.SELECT_STAGE);
                if (SelectStageItemInfo.OpenStageCount < StaticValues.MAX_STAGE)
                    SelectStageItemInfo.OpenStageCount++;
            }
        }

        public void Release()
        {
            m_GameManager.RemoveAll();
            Updater.GetInstance().Release();
            RenderDrawer.GetInstance().Release();
        }
    }

}
